package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.ReportBalance;
import com.sc.nft.entity.dto.ReportBalanceAndPayBalanceDTO;
import com.sc.nft.entity.dto.ReportSaveDTO;
import com.sc.nft.entity.vo.AdminReportBalanceVO;
import com.sc.nft.enums.DaoLevelTypeEnum;
import com.sc.nft.enums.StatisticalStatementEnum;
import com.sc.nft.helper.ReportOverviewHelper;
import com.sc.nft.service.ReportBalanceService;
import com.sc.nft.service.ReportOverviewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-02-16 15:00:22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ReportBalanceServiceImpl implements ReportBalanceService {

    private final UserWalletDao userWalletDao;

    private final DepositWithdrawalRecordDao depositWithdrawalRecordDao;

    private final SandTransferRecordDao sandTransferRecordDao;

    private final PayOrderDao payOrderDao;

    private final SecondOrderDao secondOrderDao;

    private final UserWalletDetailDao userWalletDetailDao;

    private final OrderDao orderDao;

    private final StaticDataDao staticDataDao;

    private final ReportBalanceDao reportBalanceDao;

    private final AirdropGrantRecordDao airdropGrantRecordDao;

    private final UserSynthesisRecordDao userSynthesisRecordDao;

    private final ReportOverviewService reportOverviewService;
    private final DigitalCollectionDao digitalCollectionDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;

    @Override
    public void insertReportBalance() {
        ReportBalance reportBalance = new ReportBalance();



        //获取平台金额和红包金额
        ReportBalanceAndPayBalanceDTO reportBalanceAndPayBalanceDTO = userWalletDao.getSumBalanceAndSumNowPayBalance();
        reportBalance.setSumBalance(reportBalanceAndPayBalanceDTO.getSumBalance());
        reportBalance.setSumNowPayBalance(reportBalanceAndPayBalanceDTO.getSumNowPayBalance());
        //累积已充值
        reportBalance.setSumRecharge(depositWithdrawalRecordDao.getSumRecharge());
        BigDecimal sumWithdraw = depositWithdrawalRecordDao.getSumWithdraw();
        BigDecimal sumTransfer = sandTransferRecordDao.getSumTransfer();
        //累积已转账(提现+转账)
        reportBalance.setSumTransfer(sumWithdraw.add(sumTransfer));
        //银行卡支付总额
        BigDecimal sumPayAmount = payOrderDao.getSumPayAmount();
        //转账手续费
        BigDecimal sumCharge = sandTransferRecordDao.getSumCharge();
        //充值金额
        BigDecimal sumRechargeAmount = depositWithdrawalRecordDao.getSumRechargeAmount();

        //  (一级市场支付总额 + 充值金额) * 千6  = 支付手续费
        //  支付手续费 + 转账手续费 = 手续费
        BigDecimal toDayCharge = sumPayAmount.add(sumRechargeAmount).multiply(new BigDecimal("0.006"));
        reportBalance.setSumOutCharge(toDayCharge.add(sumCharge));

        //二级市场收取的手续费
        reportBalance.setSumInCharge(secondOrderDao.getSumInCharge());
        //累计已发放红包
        reportBalance.setSumOutPayBalance(userWalletDetailDao.getSumPayBalance(Boolean.TRUE));
        //累积已消费红包
        reportBalance.setSumUsePayBalance(userWalletDetailDao.getSumPayBalance(Boolean.FALSE));

        // 居民权益卡藏品id.
        List<Long> residentCollectionDaoEquityId = digitalCollectionDao.getEquityId(DaoLevelTypeEnum.RESIDENT);

        // 获取居民权益卡商品id.
        List<Long> residentProductInfoDaoEquityId = digitalCollectionProductInfoDao.getEquityId(residentCollectionDaoEquityId);


        // 城主权益卡藏品id.
        List<Long> castellanCollectionDaoEquityId = digitalCollectionDao.getEquityId(DaoLevelTypeEnum.CITY_LORD);

        // 城主卡权益商品id
        List<Long> castellanProductInfoDaoEquityId = digitalCollectionProductInfoDao.getEquityId(castellanCollectionDaoEquityId);

        //获取城主销售数量
        reportBalance.setNowCityLordSellCount(orderDao.getSumCountByProductId(castellanProductInfoDaoEquityId));
        //获取居民销售数量
        reportBalance.setNowResidentSellCount(orderDao.getSumCountByProductId(residentProductInfoDaoEquityId));


        //空投数量
        reportBalance.setSumAirdropGrantResidentCount(airdropGrantRecordDao.getAllCountByCollectionId(residentCollectionDaoEquityId));
        reportBalance.setSumAirdropGrantCityLordCount(airdropGrantRecordDao.getAllCountByCollectionId(castellanCollectionDaoEquityId));
        //合成数量
        reportBalance.setSumSynthesisCityLordCount(userSynthesisRecordDao.getSumCountByCollectionId(castellanCollectionDaoEquityId));
        reportBalance.setSumSynthesisResidentCount(userSynthesisRecordDao.getSumCountByCollectionId(residentCollectionDaoEquityId));
        reportBalance.insert();
    }

    @Override
    public void insertReportBalanceNewEdition() {
        ReportBalance reportBalance = new ReportBalance();

        // 所有数据,一起插入.
        List<ReportSaveDTO> list = new ArrayList<>();

        //获取平台金额和红包金额
        ReportBalanceAndPayBalanceDTO reportBalanceAndPayBalanceDTO = userWalletDao.getSumBalanceAndSumNowPayBalance();
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.TOTAL_BALANCE_OF_PLATFORM_ACCOUNT,  reportBalanceAndPayBalanceDTO.getSumBalance());
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.PLATFORM_RED_ENVELOPE_RED_ENVELOPE_BALANCE,  reportBalanceAndPayBalanceDTO.getSumNowPayBalance());

        //累积已充值
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_RECHARGE,  depositWithdrawalRecordDao.getSumRecharge());
        BigDecimal sumWithdraw = depositWithdrawalRecordDao.getSumWithdraw();
        BigDecimal sumTransfer = sandTransferRecordDao.getSumTransfer();
        //累积已转账(提现+转账)
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_TRANSFERRED,  sumWithdraw.add(sumTransfer));
        //银行卡支付总额
        BigDecimal sumPayAmount = payOrderDao.getSumPayAmount();
        //转账手续费
        BigDecimal sumCharge = sandTransferRecordDao.getSumCharge();
        //充值金额
        BigDecimal sumRechargeAmount = depositWithdrawalRecordDao.getSumRechargeAmount();

        //  (一级市场支付总额 + 充值金额) * 千6  = 支付手续费
        //  支付手续费 + 转账手续费 = 手续费
        BigDecimal toDayCharge = sumPayAmount.add(sumRechargeAmount).multiply(new BigDecimal("0.006"));
        reportBalance.setSumOutCharge(toDayCharge.add(sumCharge));
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_COMMISSION_EXPENSES,  toDayCharge.add(sumCharge));
        //二级市场收取的手续费
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_FEE_INCOME,  secondOrderDao.getSumInCharge());
        //累计已发放红包
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_RED_ENVELOPES_ISSUED,  userWalletDetailDao.getSumPayBalance(Boolean.TRUE));
        //累积已消费红包
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.ACCUMULATED_RED_ENVELOPES_USED,  userWalletDetailDao.getSumPayBalance(Boolean.FALSE));

        // 居民权益卡藏品id.
        List<Long> residentCollectionDaoEquityId = digitalCollectionDao.getEquityId(DaoLevelTypeEnum.RESIDENT);

        // 获取居民权益卡商品id.
        List<Long> residentProductInfoDaoEquityId = digitalCollectionProductInfoDao.getEquityId(residentCollectionDaoEquityId);


        // 城主权益卡藏品id.
        List<Long> castellanCollectionDaoEquityId = digitalCollectionDao.getEquityId(DaoLevelTypeEnum.CITY_LORD);

        // 城主卡权益商品id
        List<Long> castellanProductInfoDaoEquityId = digitalCollectionProductInfoDao.getEquityId(castellanCollectionDaoEquityId);

        //获取城主销售数量
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_SALES_VOLUME_OF_CITY_MASTER,  Convert.toStr(orderDao.getSumCountByProductId(castellanProductInfoDaoEquityId)));
        //获取居民销售数量
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_RESIDENT_SALES_VOLUME,  Convert.toStr(orderDao.getSumCountByProductId(residentProductInfoDaoEquityId)));



        //空投数量
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_NUMBER_OF_AIRDROPPED_RESIDENTS,  Convert.toStr(airdropGrantRecordDao.getAllCountByCollectionId(residentCollectionDaoEquityId)));
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_AIR_DROP_CITY_MASTER_SALES_VOLUME,  Convert.toStr(airdropGrantRecordDao.getAllCountByCollectionId(castellanProductInfoDaoEquityId)));
        //合成数量
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_COMPOSITE_NUMBER_OF_CITY_LORDS,  Convert.toStr(userSynthesisRecordDao.getSumCountByCollectionId(castellanProductInfoDaoEquityId)));
        ReportOverviewHelper.generalReportingMethod(list,StatisticalStatementEnum.CUMULATIVE_COMPOSITE_NUMBER_OF_RESIDENTS,  Convert.toStr(userSynthesisRecordDao.getSumCountByCollectionId(residentCollectionDaoEquityId)));
        reportOverviewService.saveBath(list);
    }



    @Override
    public AdminReportBalanceVO getLastOneDay() {
        return BeanUtil.copyProperties(reportBalanceDao.getLastOneDay(), AdminReportBalanceVO.class);
    }


 
}